package com.ruoyi.alert.mq;

import com.ruoyi.alert.domain.Alert;
import com.ruoyi.alert.service.DingTalkService;
import com.ruoyi.alert.service.impl.AlertServiceImpl;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AlertConsumer {

    @Autowired
    private AlertServiceImpl alertService;

    @Autowired
    private DingTalkService dingTalkService;

    @RabbitListener(queues = "alarm.queue",messageConverter = "producerMessageConverter")
    public void processAlarm(Alert message) {
        try {
            System.out.println("接收到警报消息: " + message);
            boolean isDuplicate = alertService.isDuplicate(message);
//            System.out.println("是否重复消息: " + isDuplicate);
            if (!alertService.isDuplicate(message)) {
                System.out.println("准备发送钉钉消息...");
                // 钉钉通知
                dingTalkService.sendAlarm(message);
                alertService.markAsSent(message);

            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("处理警报失败", e);
        }
    }
}